package com.jcl.cloud.services.crawler.catchnew.util;

import com.jcl.cloud.services.crawler.catchnew.constant.TaskInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.NotOLE2FileException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.BufferUnderflowException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileUtil {
    private static Logger logger = LoggerFactory.getLogger(FileUtil.class);



    public static List<Map> resolveExcel(File file) {
        InputStream inputStream = null;
        Workbook wb = null;
        Map<String, String> map = null;
        List<Map> infoList = new ArrayList<>();

        try {
            if (file.getName().endsWith("xls")) {
                inputStream = new FileInputStream(file);
                wb = new HSSFWorkbook(inputStream);
            } else {
                return null;
            }

            Sheet userListSheet = wb.getSheetAt(0);
            int lastRowNum = userListSheet.getLastRowNum();

            for (int i = 1; i <= lastRowNum; i++) {
                map = new HashMap<>();
                try {
                    String realname = userListSheet.getRow(i).getCell(0).getStringCellValue();
                    map.put("realname", realname);
                    String resumeId = userListSheet.getRow(i).getCell(2).getStringCellValue();
                    String apply_other_jid = TaskInfo.rid2jidMap.remove(resumeId);
                    map.put("apply_other_jid", "51job" + apply_other_jid);
                    String sex = userListSheet.getRow(i).getCell(7).getStringCellValue();
                    map.put("sex", sex);
                    String mobile = userListSheet.getRow(i).getCell(15).getStringCellValue();
                    map.put("mobile", mobile);
                    String email = userListSheet.getRow(i).getCell(16).getStringCellValue();
                    map.put("email",email);
                    String birthday = userListSheet.getRow(i).getCell(8).getStringCellValue();
                    map.put("birthday", birthday + "-00");
                    String current_city = userListSheet.getRow(i).getCell(9).getStringCellValue();
                    map.put("current_city", StringUtil.getCityName(current_city));
                    String current_address = userListSheet.getRow(i).getCell(9).getStringCellValue();
                    map.put("current_address", current_address);
                    String workyear = userListSheet.getRow(i).getCell(11).getStringCellValue();
                    map.put("workyear", StringUtil.getNumFromStr(workyear));
                    String work_industry = "未知";
                    map.put("work_industry", work_industry);
                    String work_last_position = userListSheet.getRow(i).getCell(20).getStringCellValue();
                    map.put("work_last_position", work_last_position);
                    String work_last_company = userListSheet.getRow(i).getCell(19).getStringCellValue();
                    map.put("work_last_company", work_last_company);
                    String work_detail = wb.getSheetAt(i).getRow(41).getCell(2).getStringCellValue();
                    map.put("work_detail", work_detail);


                    String edu_last_education = userListSheet.getRow(i).getCell(12).getStringCellValue();
                    map.put("edu_last_education", edu_last_education);
                    String edu_last_school = userListSheet.getRow(i).getCell(13).getStringCellValue();
                    map.put("edu_last_school", edu_last_school);
                    String edu_last_major = userListSheet.getRow(i).getCell(14).getStringCellValue();
                    map.put("edu_last_major", edu_last_major);
                        /*String source = "未知";
                        map.put("source", source);*/
                    String source_sub = "51job";
                    map.put("source_sub", source_sub);
                    String apply_status = userListSheet.getRow(i).getCell(23).getStringCellValue();
                    map.put("apply_status", apply_status);
                    String apply_department = userListSheet.getRow(i).getCell(4).getStringCellValue();
                    map.put("apply_department", apply_department);
                    String apply_position = userListSheet.getRow(i).getCell(3).getStringCellValue();
                    map.put("apply_position", apply_position);
                    String apply_date = userListSheet.getRow(i).getCell(6).getStringCellValue();
                    map.put("apply_date", apply_date);
                    String status = "";
                    map.put("status", status);

                    String edu_detail = wb.getSheetAt(i).getRow(45).getCell(2).getStringCellValue();
                    map.put("edu_detail",edu_detail );
                    String project_detail = wb.getSheetAt(i).getRow(43).getCell(2).getStringCellValue();
                    map.put("project_detail",project_detail );
                    String update_date = wb.getSheetAt(i).getRow(5).getCell(2).getStringCellValue();
                    map.put("update_date",update_date );
                    String arrival_date = wb.getSheetAt(i).getRow(37).getCell(2).getStringCellValue();
                    map.put("arrival_info",arrival_date );
                    String evaluate = wb.getSheetAt(i).getRow(39).getCell(2).getStringCellValue();
                    map.put("evaluate",evaluate );
                    String current_salary = wb.getSheetAt(i).getRow(26).getCell(2).getStringCellValue();
                    map.put("current_salary",current_salary );
                    String desire_salary = wb.getSheetAt(i).getRow(33).getCell(2).getStringCellValue();
                    //map.put("desire_salary",desire_salary );

                    String[] salaryArr = StringUtil.getSalary(desire_salary);
                    map.put("desire_salary_start", salaryArr[0] );
                    map.put("desire_salary_end", salaryArr[1] );


                } catch (Exception e) {
                    logger.error("excel中获取字段值发生异常：", e);
                }

                infoList.add(map);
            }

        } catch (BufferUnderflowException bufe) {
            logger.error("解析文档发生的异常为：", bufe);
            try {
                file.delete();
            } catch (Exception e) {
                logger.error("解析文档发生BufferUnderflowException异常后删除文档发生异常：", e);
            }
        } catch (NotOLE2FileException not2fe) {
            logger.error("解析文档发生的异常为：", not2fe);
            try {
                file.delete();
            } catch (Exception e) {
                logger.error("解析文档发生NotOLE2FileException异常后删除文档发生异常：", e);
            }
        } catch (IndexOutOfBoundsException ioobe) {
            logger.error("解析文档发生的异常为：", ioobe);
            try {
                file.delete();
            } catch (Exception e) {
                logger.error("解析文档发生IndexOutOfBoundsException异常后删除文档发生异常：", e);
            }
        } catch (Exception e) {
            logger.error(String.format("解析文档发生异常："), e);
        } finally {
            try {
                wb.close();
                inputStream.close();
            } catch (Exception e) {
                logger.error("资源关闭发生异常：", e);
            }
        }


        return infoList;
    }
}
